#include "frmmain.h"
#include "ui_frmmain.h"
#include <QtCore>
#include <QtGui>
#include "iconhelper.h"
#include "quihelper.h"
#include "devicesizetable.h"
#include "frmbar.h"
#include "frmabout.h"
#include "frmdice.h"
#include "frmcourse.h"
#include "main_menu.h"
#include "settings/frmsetting.h"
#include "gamegobang/pvpwindow.h"
#include "connection.h"
#include <QSharedMemory>

frmMain::frmMain(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::frmMain)
{
    ui->setupUi(this);
    initForm();
    initStyle();
    initLeftMain();
}

frmMain::~frmMain()
{
    delete ui;
}

bool frmMain::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==ui->widgetTitle)
    {
        if(event->type()==QEvent::MouseButtonDblClick)
        {
            on_btnMenu_Max_clicked();
        }
    }

    return QWidget::eventFilter(watched, event);
}

void frmMain::resizeEvent(QResizeEvent *event)
{
    int width=ui->widgetCenterPage1->width();
    qDebug()<<this->width();
    qDebug()<<width;
}

void frmMain::closeEvent(QCloseEvent *event)
{
    closeConnection();
}

void frmMain::getQssColor(const QString &qss, const QString &flag, QString &color)
{
    int index=qss.indexOf(flag);
    if(index>=0)
    {
        color=qss.mid(index+flag.length(),7);
    }
}

void frmMain::getQssColor(const QString &qss, QString &textColor, QString &panelColor,
                          QString &borderColor, QString &normalColorStart,
                          QString &normalColorEnd, QString &darkColorStart, QString &darkColorEnd, QString &highColor)
{
    getQssColor(qss,"TextColor:",textColor);
    getQssColor(qss, "PanelColor:", panelColor);
    getQssColor(qss, "BorderColor:", borderColor);
    getQssColor(qss, "NormalColorStart:", normalColorStart);
    getQssColor(qss, "NormalColorEnd:", normalColorEnd);
    getQssColor(qss, "DarkColorStart:", darkColorStart);
    getQssColor(qss, "DarkColorEnd:", darkColorEnd);
    getQssColor(qss, "HighColor:", highColor);
}

void frmMain::initForm()
{
    QUIHelper::setFormInCenter(this);
    //设置无边框
    QUIHelper::setFramelessForm(this);
    //设置图标
    IconHelper::setIcon(ui->labIco,0xe653,30);
    IconHelper::setIcon(ui->btnMenu_Min, 0xe640);
    IconHelper::setIcon(ui->btnMenu_Max, 0xe641);
    IconHelper::setIcon(ui->btnMenu_Close, 0xe642);
    IconHelper::setIcon(ui->btnMenu_List,0xe64e);

    //设置样式
    ui->widgetTitle->setProperty("form","title");
    ui->widgetTitle->installEventFilter(this);
    ui->widgetTop->setProperty("nav","top");

    //ui->stackedWidget->setStyleSheet("QLabel{font:60px;}");

    QFont font;
    font.setPixelSize(25);
    ui->labTitle->setFont(font);
    ui->labTitle->setText("数据查询平台");
    int lbWidth=ui->labTitle->geometry().width();
    int lbHeight=ui->labTitle->geometry().height();
    ui->labTitle->setGeometry(ui->labIco->geometry().right()+10,0,lbWidth,lbHeight);
    this->setWindowTitle(ui->labTitle->text());

    //设置顶部导航按钮
    QSize icoSize(32, 32);
    int icoWidth = 85;
    QList<QAbstractButton *> tbtns=ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setIconSize(icoSize);
        btn->setMinimumWidth(icoWidth);
        btn->setCheckable(true);
        connect(btn,SIGNAL(clicked()),this,SLOT(buttonClick()));
    }

    this->initMainMenu();

    ui->widgetLeftMain->setProperty("flag","left");
    //ui->page1->setStyleSheet(QString("QWidget[flag=\"left\"] QAbstractButton{min-height:%1px;max-height:%1px}").arg(20));
    ui->page2->setStyleSheet(QString("QLabel{font:%1px}").arg(14));

    ui->page2_horlayout->addWidget(new FrmBar);
    ui->page3_horlayout->addWidget(new FrmSetting);


    ui->btnHome->click();
}

//加载主菜单
void frmMain::initMainMenu()
{
    this->main_menu=new MainMenu();
    connect(this->main_menu,SIGNAL(showAboutus()),this,SLOT(menu_Aboutus()));
    connect(this->main_menu,SIGNAL(showHelp()),this,SLOT(menu_help()));
    connect(this->main_menu,SIGNAL(showDice()),this,SLOT(menu_showDice()));
}

void frmMain::menu_help()
{
    QUIHelper::showMessageBoxInfo("QT开源论坛！");
}

void frmMain::menu_Aboutus()
{
    FrmAbout *frm=new FrmAbout;
    frm->setWindowModality(Qt::WindowModal);
    frm->setWindowFlags(Qt::WindowCloseButtonHint);
    frm->setWindowTitle(QString("关于"));
    frm->show();
}

void frmMain::menu_showDice()
{
    FrmDice *frm=new FrmDice;
    frm->setWindowFlags(Qt::WindowCloseButtonHint);
    frm->setWindowModality(Qt::WindowModal);
    frm->setWindowTitle(QString("掷骰子"));
    frm->show();
}


void frmMain::initStyle()
{
    //加载样式表
    QString qss;
    QFile file(":/qss/blacksoft.css");
    if(file.open(QFile::ReadOnly))
    {
        qss=QLatin1String(file.readAll());
        QString palatteColor=qss.mid(20,7);
        qApp->setPalette(QPalette(palatteColor));
        qApp->setStyleSheet(qss);
        file.close();
    }

    //先从样式表中取出对应的颜色
    QString textColor, panelColor, borderColor, normalColorStart, normalColorEnd, darkColorStart, darkColorEnd, highColor;
    getQssColor(qss, textColor, panelColor, borderColor, normalColorStart, normalColorEnd, darkColorStart, darkColorEnd, highColor);

    //将对应的颜色设置到控件
    this->borderColor=highColor;
    this->normalBgColor=normalColorStart;
    this->darkBgColor=darkBgColor;
    this->normalTextColor=textColor;
    this->darkTextColor=darkColorStart;
}

void frmMain::buttonClick()
{
    QAbstractButton *b=(QAbstractButton *) sender();
    QString name=b->text();

    QList<QAbstractButton *> tbtns=ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setChecked(btn==b);
    }
    if(name=="主界面")
    {
        ui->stackedWidget->setCurrentIndex(0);
    }
    else if(name=="数据查询")
    {
        ui->stackedWidget->setCurrentIndex(1);
    }
    else if(name=="系统设置")
    {
        ui->stackedWidget->setCurrentIndex(2);
    }
    else if(name=="系统退出")
    {
        exit(0);
    }
}

void frmMain::initLeftConfig()
{

}

void frmMain::initLeftMain()
{
    iconsMain << 0xe675 << 0xe677<<0xe679<<0xe679;
    btnsMain << ui->btnMain1 << ui->btnMain2<<ui->btnMainGobang<<ui->btnMainCourse;

    int count = btnsMain.count();
    for (int i = 0; i < count; ++i) {
        QToolButton *btn = (QToolButton *)btnsMain.at(i);
        btn->setCheckable(true);
        btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        connect(btn, SIGNAL(clicked(bool)), this, SLOT(leftMainClick()));
    }

    IconHelper::StyleColor styleColor;
    styleColor.position = "left";
    styleColor.iconSize = 18;
    styleColor.iconWidth = 35;
    styleColor.iconHeight = 20;
    styleColor.borderWidth = 4;
    styleColor.borderColor = borderColor;
    styleColor.setColor(normalBgColor, normalTextColor, darkBgColor, darkTextColor);
    IconHelper::setStyle(ui->widgetLeftMain, btnsMain, iconsMain, styleColor);
    ui->btnMain1->click();
}

void frmMain::leftMainClick()
{
    QAbstractButton *b=(QAbstractButton *) sender();
    QString name=b->text();

    int count=btnsMain.count();
    for(int i=0;i<count;i++)
    {
        QAbstractButton *btn=btnsMain.at(i);
        btn->setChecked(btn==b);
    }
    if(name=="磁盘监控")
    {
        ui->tableView->loadDiskMonitor();
    }
    if(name=="人员监控")
    {
        ui->tableView->loadUserMonitor();
    }
    if(name=="五子棋")
    {
        PVPWindow *frm=new PVPWindow;
        frm->setWindowFlags(Qt::WindowCloseButtonHint);
        frm->setWindowTitle(QString("五子棋"));
        frm->setWindowModality(Qt::WindowModal);
        frm->show();
    }
    if(name=="排课表")
    {
       FrmCourse *frmCrs=new FrmCourse;
       frmCrs->setWindowFlags(Qt::WindowCloseButtonHint);
       frmCrs->setWindowTitle(QString("排课表"));
       frmCrs->setWindowModality(Qt::WindowModal);
       frmCrs->show();
    }
}

void frmMain::on_btnMenu_Max_clicked()
{
    static bool max = false;
    static QRect location = this->geometry();

    if (max) {
        this->setGeometry(location);
    } else {
        location = this->geometry();
        this->setGeometry(getScreenRect());
    }

    this->setProperty("canMove", max);
    max = !max;
}

QRect frmMain::getScreenRect()
{
    QRect rect;
    rect = qApp->screens().at(0)->availableGeometry();
    return rect;
}

void frmMain::on_btnMenu_Min_clicked()
{
    showMinimized();
}


void frmMain::on_btnMenu_Close_clicked()
{
    close();
}

void frmMain::on_btnMenu_List_clicked()
{
    QPoint p=rect().topRight();
    p.setX(p.x()-98);
    p.setY(p.y()+32);
    main_menu->exec(this->mapToGlobal(p));
}


